{include file="public/header" /}
<link rel="stylesheet" type="text/css" href="/static/admin/webupload/webuploader.css">
<link rel="stylesheet" type="text/css" href="/static/admin/webupload/style.css">
<style>
.file-item{float: left; position: relative; width: 110px;height: 110px; margin: 0 20px 20px 0; padding: 4px;}
.file-item .info{overflow: hidden;}
.uploader-list{width: 100%; overflow: hidden;}
</style>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑管理员</h5>
                </div>
                <div class="ibox-content">
                    <form class="layui-form layui-form-pane" action="{:url('userEdit')}">
                        <input type="hidden" name="id" value="{$user.id}">
                        <div class="layui-form-item">
                            <label class="layui-form-label">登录账号</label>
                            <div class="layui-input-inline">
                                <input id="username" type="text" class="layui-input" name="username" required="" aria-required="true" value="{$user.username}">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">角&emsp;&emsp;色</label>
                            <div class="layui-input-inline">
                                <select class="layui-input m-b chosen-select" name="groupid" id="groupid">
                                    <option value="">==请选择角色==</option>
                                    {if !empty($role)}
                                        {foreach name="role" item="vo"}
                                            <option value="{$vo.id}" {if condition="$user['groupid'] eq $vo['id']"}selected{/if}>{$vo.title}</option>
                                        {/foreach}
                                    {/if}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">头&emsp;&emsp;像</label>
                            <div class="layui-input-inline">
                                <input type="hidden" id="data_photo" name="portrait" value="{$user.portrait}" />
                                <div id="fileList" class="uploader-list" style="float:right"></div>
                                <div id="imgPicker" style="float:left">选择头像</div>
                                <img id="img_data" class="img-circle" height="80px" width="80px" style="float:left;margin-left: 50px;" src="/static/uploads/face/{$user.portrait}" onerror="this.src='/static/admin/images/head_default.gif'"/>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">密&emsp;&emsp;码</label>
                            <div class="layui-input-inline">
                                <input id="password" type="text" class="layui-input" name="password"  placeholder="再次输入修改密码">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">姓&emsp;&emsp;名</label>
                            <div class="layui-input-inline">
                                <input id="real_name" type="text" class="layui-input" name="real_name" required="" aria-required="true" value="{$user.real_name}">

                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">状&emsp;&emsp;态</label>
                            <div class="col-sm-6">
                                <div class="radio i-checks">
                                    <input type="radio" name='status' value="1" {if condition="$user['status'] eq 1 "}checked{/if}/>开启&nbsp;&nbsp;
                                    <input type="radio" name='status' value="0" {if condition="$user['status'] eq 0 "}checked{/if}/>关闭
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <button class="btn btn-primary" type="submit" lay-submit lay-filter="user"><i class="fa fa-save"></i> 保存</button>&nbsp;&nbsp;&nbsp;
                                <a class="btn btn-danger" href="javascript:history.go(-1);"><i class="fa fa-close"></i> 返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>
{include file="public/footer" /}
<script type="text/javascript" src="/static/admin/webupload/webuploader.min.js"></script>
<script type="text/javascript">
    var $list = $('#fileList');
    //上传图片,初始化WebUploader
    var uploader = WebUploader.create({
     
        auto: true,// 选完文件后，是否自动上传。   
        swf: '/static/admin/webupload/Uploader.swf',// swf文件路径
        server: "{:url('Upload/uploadface')}",// 文件接收服务端。
        duplicate :true,// 重复上传图片，true为可重复false为不可重复
        pick: '#imgPicker',// 选择文件的按钮。可选。

        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/jpg,image/jpeg,image/png'
        },

        'onUploadSuccess': function(file, data, response) {
            $("#data_photo").val(data._raw);
            $("#img_data").attr('src', '/static/uploads/face/' + data._raw).show();
        }
    });

    uploader.on( 'fileQueued', function( file ) {
        $list.html( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">正在上传...</p>' +
        '</div>' );
    });

    // 文件上传成功
    uploader.on( 'uploadSuccess', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传成功！');
    });

    // 文件上传失败，显示上传出错。
    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错!');
    });

    var form = layui.form;
    form.on('submit(user)', function (data) {
        var data = data.field;
        if (!data.username) {
            layer.msg('请输入登录账号', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                layer.close(index);
            });
            return false;
        }

        if (!data.groupid) {
            layer.msg('请选择用户角色', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                layer.close(index);
            });
            return false;
        }

        if (!data.password) {
            layer.msg('请输入登录密码', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                layer.close(index);
            });
            return false;
        }

        if (!data.real_name) {
            layer.msg('请输入真实姓名', {icon: 2, time: 1500, shade: 0.1}, function (index) {
                layer.close(index);
            });
            return false;
        }
        $.post("{:url('userAdd')}", data, function (result) {
            if (result.code == 1) {
                layer.msg(result.msg, {icon: 6, time: 1500, shade: 0.1}, function (index) {
                    window.location.href = "{:url('user/index')}";
                });
            } else {
                layer.msg(result.msg, {icon: 5, time: 1500, shade: 0.1});
                return false;
            }
        });
        return false;
    });
</script>
</body>
</html>